<?php

namespace app\common\model\ads;

use think\Model;

/**
 * 广告评论点赞
 */
class CommentZan extends Model
{

    // 表名
    protected $name = 'self_comment_zan';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;


   // 追加属性
    protected $append = [
        // 'createtime_text'
    ];

    
    public function getCreatetimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['createtime']) ? $data['createtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i", $value) : $value;
    }

    public static function zan($comment_id, $user_id){
        if($comment_id && $user_id){
            return self::create([
                'user_id' => $user_id,
                'comment_id' => $comment_id,
            ]);
        }
        return false;
    }

    public static function isZan($comment_id, $user_id){
        return self::where([
            'user_id' => $user_id,
            'comment_id' => $comment_id,
        ])->find() ? 1 : 0;
    }

}
